#!/bin/bash

# 功能：使用本目录下的makefile文件编译驱动模块并复制到tftp目录中
# 用法：./BuildDriverAndCopy.sh (不用添加文件名，执行的是makefile文件)

#设置目标目录
TARGET_DIR="/home/nixiak/linux/tftp"

# 确保目录存在
if [ ! -d "$TARGET_DIR" ]; then
    echo "Error: Target directory $TARGET_DIR does not exist."
    exit 1
fi

# 开始编译
echo "Running make..."
make
if [ $? -ne 0 ]; then
    echo "Error: Make command failed. Exiting."
    exit 1
fi

# 移动模块到tftp目录中
echo "Copying .ko files to $TARGET_DIR..."
find . -maxdepth 1 -name "*.ko" -exec cp {} "$TARGET_DIR" \;
if [ $? -ne 0 ]; then
    echo "Error: Failed to copy .ko files. Exiting."
    exit 1
fi

echo "All .ko files successfully copied to $TARGET_DIR."